@push('title')
    <title>航班数据</title>
@endpush
@push('style2')
<style>


    .layui-table-cell{
        height: auto;
        overflow:visible;
        text-overflow:inherit;
        white-space:normal;
        word-break: break-all;
    }
</style>
@endpush
@extends('layout.layer')
@section('content')

    <form class="layui-form layui-form-pane1" action="" lay-filter="first">

        <div class="layui-input-inline">
            <label class="layui-form-label">航司任务名称</label>
            <div class="layui-input-inline">
                <input type="text" name="hsCode" maxlength="" placeholder="" autocomplete="off" class="layui-input" >
            </div>
        </div>

        <div class="layui-input-inline">
            <button class="layui-btn" id="search-btn" lay-submit lay-filter="*">搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </form>
    <table id="test2" lay-filter="test2" style="margin-top: 20px"></table>
    <script type="text/html" id="result-option">

        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="download">下载</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="policy-upload">上传政策</a>

    </script>
@endsection
@push('script')
    <script>

        layui.use(['table','form','laydate'], function() {
            var $ = layui.$
                ,table = layui.table
                ,form = layui.form
                ,laydate = layui.laydate;


            //日期
            laydate.render({
                elem: '#rob-start'
                ,type: 'datetime'
                , format: 'yyyy-MM-dd HH:mm:ss',    //控制文本显示的内容格式
            });

            //日期
            laydate.render({
                elem: '#rob-end'
                ,type: 'datetime'
                , format: 'yyyy-MM-dd HH:mm:ss',    //控制文本显示的内容格式
            });


            var tableData = {};

            table.render({
                id: 'test2'
                ,elem: '#test2'
                ,url: '/javaapi/data/crmTasks/query'
                ,method: 'post'
                ,contentType: "application/json;charset=UTF-8" // 设置请求的Content-Type
                ,page: { //详细参数可参考 laypage 组件文档
                    curr: 1
                    ,groups: 20
                    ,first: 1
                    ,last: true
                    ,layout: ['limit', 'prev', 'page', 'next', 'count','refresh', 'skip'] //自定义分页布局
                }
                //,height: 300
                ,cellMinWidth: 80
                ,limit : 30
                //,skin: 'line'
                ,toolbar: false
                ,parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                    tableData =  res.data

                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data":res.data.records,
                    };
                }
                ,request: {
                    pageName: 'pageIndex' //页码的参数名称，默认：page
                    ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
                }
                ,cols: [[
                    {field: 'id', 'title':'ID'}
                    ,{field: 'hsCode', 'title':'航司任务名'}
                    ,{field: 'startDate', 'title':'开始日期'}
                    ,{field: 'endDate', 'title':'结束日期'}
                    ,{field: 'updatedAt', 'title':'开始抓数据时间',templet: function (row) {
                            return row.createdAt.replace("T","");
                        }}
                    ,{field: 'updatedAt', 'title':'开始抓数据时间',templet: function (row) {
                            return row.createdAt.replace("T","");
                        }}
                    ,{field: 'status', 'title':'状态',templet: function (row) {
                            switch (row.status) {
                                case 1: return '待开始';
                                case 2: return '进行中';
                                case 3: return '已完成';
                            }
                        }}
                    ,{field:'option', title:'操作',toolbar: '#result-option'}
                ]]

            });

            //监听提交
            form.on('submit(*)', function(data){

                table.reload('test2',{
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where:data.field
                });

                return false;
            });


            //监听工具条
            table.on('tool(test2)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值


                if (layEvent === 'download') {


                    fetch('/javaapi/data/av_flights/av_flights_ota_hu/export', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        timeout: 300000,
                        body: JSON.stringify({taskId:data.id}),
                    })
                        .then(response => {
                            if (!response.ok) {
                                throw new Error('Network response was not ok');
                            }
                            return response.blob();
                        })
                        .then(blob => {
                            // 创建一个可下载的链接
                            const url = window.URL.createObjectURL(blob);
                            const link = document.createElement('a');
                            link.href = url;
                            link.setAttribute('download', 'av_flights_ota_hu.xlsx'); // 文件名
                            document.body.appendChild(link);
                            link.click();
                            document.body.removeChild(link);
                        })
                        .catch(error => {
                            console.error('导出失败:', error);
                        });
                } else if (layEvent === 'policy-upload') {

                    layer.confirm('任务ID：'+ data.id, {icon: 1, title:'确认上传', btn: ['确定','取消'],yes:function () {

                            $.ajax({
                                url: '/javaapi/data/crmTasks/rePushAppData?taskId=' + data.id
                                , method: 'get'
                                , success: function (data) {

                                    layer.msg(data.msg, {
                                        offset: '15px'
                                        , icon: (data.code === 0 ? 1 : 5)
                                        , time: 2000
                                    }, function () {
                                    });
                                },
                            });
                        }
                    });
                }

            })

        });
    </script>

@endpush